package com.quik.web.room.action;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.quik.common.Constants;
import com.quik.dto.PaginationDto;
import com.quik.entity.RoomArea;
import com.quik.entity.SysAccount;
import com.quik.exception.ErrorCode;
import com.quik.web.room.service.RoomService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

/**
 * 功能说明：
 *         房间区域的业务控制类
 * @author jenny
 *
 */
@Controller
@RequestMapping("/roomArea")
public class RoomAreaController {
	private static Logger log = (Logger)Logger.getLogger(RoomAreaController.class);
	
	@Autowired
	private RoomService roomService;
	
	/**
	 * 进入房间区域查询界面
	 * @return
	 */
	@RequestMapping(value = "roomAreaList", method = RequestMethod.GET)
	public String roomAreaList(){
		return "/room/roomArea/roomAreaList";
	}
	
	/**
	 * 查询房间区域列表
	 * @param roomAreaName
	 * @return
	 */
	@RequestMapping(value = "queryRoomArea", method = RequestMethod.POST)
	@ResponseBody
	public PaginationDto queryRoomArea(Integer page, Integer rows, String roomAreaName){
		PaginationDto dto = new PaginationDto(0, null);
		
		try{
			int intPage = (page == null || page == 0) ? 1:page;  //当前页
		    int pageSize = (rows == null || rows == 0) ? Constants.TREE_PAGE_SIZE : rows;  //每页显示的记录
		    
		    int totalCount = roomService.findRoomAreaCount(roomAreaName);
		    
		    List list = roomService.findRoomArea(roomAreaName, intPage, pageSize);
		    
		    dto.setRows(list);
		    dto.setTotal(totalCount);
		}catch(Exception e){
			e.printStackTrace();
			log.error("RoomAreaController queryRoomArea error: " + e.getMessage());
		}
		
		return dto;
	}
	
	/**
	 * 进入新增房间区域界面
	 * @return
	 */
	@RequestMapping(value = "initAddRoomArea")
	public String initAddRoomArea(){
		return "/room/roomArea/addRoomArea";
	}
	
	/**
	 * 检查房间区域是否同名
	 * @param roomAreaName
	 * @return
	 */
	@RequestMapping(value = "checkRoomAreaName", method = RequestMethod.POST)
	public ResponseEntity<String> checkRoomAreaName(String roomAreaName){
		boolean flag = false;
		
		try{
			flag = roomService.checkRoomAreaName(roomAreaName);
			
		}catch(Exception e){
			e.printStackTrace();
			log.error("RoomAreaController checkRoomAreaName error: " + e.getMessage());
		}
		
		return new ResponseEntity<String>(String.valueOf(flag), HttpStatus.OK);
	}
	
	/**
	 * 新增房间区域
	 * @param roomArea
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addRoomArea", method = RequestMethod.POST)
	public ModelAndView addRoomArea(RoomArea roomArea, HttpServletRequest request){
		SysAccount account = (SysAccount)request.getSession().getAttribute(Constants.SESSION_ACCOUNT);
		ModelAndView mav = new ModelAndView("/room/roomArea/addRoomArea");
		String result = ErrorCode.RESULT_ADD_ROOM_AREA_FAILED;
		
		try{
			roomArea.setCrt_user(account.getStaff_no());
			roomArea.setCrt_date(new java.util.Date(System.currentTimeMillis()));
			
			roomService.addRoomArea(roomArea);
			result = ErrorCode.RESULT_ADD_ROOM_AREA_SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			log.error("RoomAreaController addRoomArea error: " + e.getMessage());
			result = ErrorCode.SYSTEM_ERROR;
		}
		
		mav.addObject("result", result);
		
		return mav;
	}

}
